//
//  JKStoreVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKStoreVC.h"

// vc
#import "JKStoreProductListVC.h"
#import "JKStoreProductDetailVC.h"

// view
#import "ByViewDetailHeaderCarousel.h"      // 轮播视图

// cell
#import "JKCellStoreIndexSections.h"
#import "JKCellStoreHotProduct.h"


// model
#import "ByModelIndexCarousel.h"


// api
#import "ByApiAppIndex.h"
#import "ByApiStore.h"

@interface JKStoreVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView                   *table;
@property (nonatomic, strong) ByViewDetailHeaderCarousel    *carousel;


// data
@property (nonatomic, copy  ) NSDictionary                  *shopsDic;
@end

@implementation JKStoreVC
#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[self.view viewWithTag:888] removeFromSuperview];
    self.title = @"唯优商城";
    [self addColorLeftBackBarButtonItem];
    [self createTableView];
    
    [self getData];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
}

#pragma mark - 请求数据
- (void)getData {
    
    [self.table.mj_header endRefreshing];
    [ByCoreToolAlert showLoadind:@"正在加载..."];
    
    dispatch_queue_t queue = dispatch_queue_create("my", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    // 轮播
    ByNetParamsModel *params = [ByNetParamsModel new];
    params.position = @"6249";
    [ByApiAppIndex getIndexCarousel:params success:^(ByNetBaseResult *result) {
        self.carousel.imageURLStringDatas = result.data;
        dispatch_group_leave(group);
    } failure:^(ByNetBaseResult *result) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    params = [ByNetParamsModel new];
    [ByApiStore getStoreHomeIndex:params success:^(ByNetBaseResult *result) {
        _shopsDic = result.data;
        dispatch_group_leave(group);
    } failure:^(ByNetBaseResult *result) {
        dispatch_group_leave(group);
    }];
    

    // 所有请求结束
    dispatch_group_notify(group, queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [ByCoreToolAlert hideHud];
            [self.table reloadData];
        });
    });
}


#pragma mark - 创建视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = [UIColor whiteColor];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    
    
    
    __weak typeof(self) wself = self;
    self.carousel = [[ByViewDetailHeaderCarousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 8 / 15) timerDuration:3];
    self.carousel.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    // 轮播点击
    self.carousel.itemDidTap = ^(NSInteger index) {
        [wself carouselTap:index];
    };
    self.table.tableHeaderView = self.carousel;
    
    
    [self.table registerClass:[JKCellStoreIndexSections class] forCellReuseIdentifier:@"sectionsCell"];
    [self.table registerClass:[JKCellStoreHotProduct class] forCellReuseIdentifier:@"hotCell"];
    [self.table registerClass:[JKCellStoreHotProduct class] forCellReuseIdentifier:@"limitCell"];
    
    
    self.table.mj_header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [wself getData];
    }];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    /*
     0.4个
     1.热门推荐
     2.限时优惠
     */
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        JKCellStoreIndexSections *cell = [tableView dequeueReusableCellWithIdentifier:@"sectionsCell"];
        cell.tapBlock = ^(NSInteger index) {
            [self sectionTap:index];
        };
        return cell;
    }
    else if (indexPath.section == 1) {
        JKCellStoreHotProduct *cell = [tableView dequeueReusableCellWithIdentifier:@"hotCell"];
        cell.products = _shopsDic[@"recommend"];
        return cell;
    }
    else if (indexPath.section == 2) {
        JKCellStoreHotProduct *cell = [tableView dequeueReusableCellWithIdentifier:@"limitCell"];
        cell.products = _shopsDic[@"discount"];
        return cell;
    }
 
    
    
    return UITableViewCell.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return [tableView fd_heightForCellWithIdentifier:@"sectionsCell" cacheByIndexPath:indexPath configuration:^(JKCellStoreIndexSections *cell) {
        }];
    }
    else if (indexPath.section == 1) {
        return [tableView fd_heightForCellWithIdentifier:@"hotCell" cacheByIndexPath:indexPath configuration:^(JKCellStoreHotProduct *cell) {
            cell.products = _shopsDic[@"recommend"];
        }];
    }
    else if (indexPath.section == 2) {
        return [tableView fd_heightForCellWithIdentifier:@"limitCell" cacheByIndexPath:indexPath configuration:^(JKCellStoreHotProduct *cell) {
            cell.products = _shopsDic[@"discount"];
        }];
    }
   

    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section > 0) {
        return 40;
    } else {
        return 0;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section > 0) {
        
        
        UIView *label = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        label.backgroundColor = TABLE_COLOR;

        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 1, SCREEN_WIDTH, 38)];
        contentLabel.backgroundColor = TABLE_COLOR;
        contentLabel.text = section == 1 ? @"热门推荐" : @"限时优惠";
        contentLabel.font = FONT(15);
        contentLabel.textAlignment = NSTextAlignmentCenter;
        contentLabel.textColor = [UIColor blackColor];
        [label addSubview:contentLabel];

        // 线条
        UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        line1.backgroundColor = [UIColor colorWithRGBHex:0xcccccc];
        [label addSubview:line1];
        UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 39, SCREEN_WIDTH, 1)];
        line2.backgroundColor = [UIColor colorWithRGBHex:0xcccccc];
        [label addSubview:line2];
        
        return label;

    } else {
        return UIView.new;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}


#pragma mark - 点击事件
// 轮播点击
- (void)carouselTap:(NSInteger)index {
    JKStoreProductDetailVC *vc = [JKStoreProductDetailVC new];
    [self by_normalPush:vc hasNavi:true];
}
// 板块点击
- (void)sectionTap:(NSInteger)index {
    
    JKStoreProductListVC *vc = [JKStoreProductListVC new];
    vc.type = index + 1;
    [self by_normalPush:vc hasNavi:true];

//    switch (index) {
//        case 0:
//        {
//
//        }
//            break;
//        case 1:
//        {
//            if (!BY_USER) {
//                ByUILoginVC *vc = [[ByUILoginVC alloc] init];
//                ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
//                [self presentViewController:navi animated:YES completion:nil];
//                return;
//            }
//        }
//            break;
//        case 2:
//        {
//        }
//            break;
//        case 3:
//        {
//        }
//            break;
//        default:
//            break;
//    }
}


@end
